<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class GroupAction extends AdminAction
{

	protected $UserGroup;
	protected $AdminGroup;


	//初始化
	public function _initialize()
	{
		parent::_initialize();
		$this->UserGroup  = D('UserGroup');
		$this->AdminGroup = D('AdminGroup');
	}


	//用户组列表
	public function manageUserGroup()
	{
		
		//系统内置组
		$where = array('type' => 'system');
		$systemGroupList = $this->UserGroup->where($where)->select();
		$this->assign('systemGroupList', $systemGroupList);
		
		//自定义会员组
		$where = array(
		  'group_id' => array('not in', array(1,2,3)),
		  'type' => 'member',
		);
		$memberGroupList = $this->UserGroup->where($where)->select();
		$this->assign('memberGroupList', $memberGroupList);

		$this->display();
	}


	//新增会员用户组
	public function addUserGroup()
	{
		if ($this->isPost())
		{
			if (false == $this->UserGroup->create())
			{
				$this->assign('jumpUrl', __URL__.'/userGroupList');
				$this->error($this->UserGroup->getError());
			}
			$data = array('group_name' => $_POST['group_name']);
			$lastID = $this->UserGroup->add($data);
			$this->UserGroup->cacheUserGroup($lastID);

			$this->assign('jumpUrl', __URL__.'/editUserGroup/group_id/'.$lastID);
			$this->success(L('group.group_added_succeed'));
		}
	}


	//编辑会员用户组
	public function editUserGroup()
	{
		
		$groupID = intval($_REQUEST['group_id']);
		$group = $this->UserGroup->find($groupID);
		$this->assign('jumpUrl', __ACTION__.'/group_id/'.$groupID);

		if (empty($group))
		{
			$this->error(L('group_does_not_exist'));
		}

		if ($this->isPost())
		{
			if (false == $this->UserGroup->create())
			{
				$this->error($this->UserGroup->getError());
			}
			if ('member' == $group['type'] && ($_POST['point_lower'] >= $_POST['point_higher']))
			{
				$this->error(L('group.group_point_range_invalid'));
			}

			$this->UserGroup->save();
			$this->UserGroup->cacheUserGroup($groupID);

			$this->success(L('group.group_updated_succeed'));
		}
		else
		{
			$boolRadios = array(1 => L('yes'), 0 => L('no'));

			$this->assign('allowDirectPostRadios', $boolRadios);
			$this->assign('allowDirectPostChecked', $group['allow_direct_post']);

			$this->assign('allowVisitRadios', $boolRadios);
			$this->assign('allowVisitChecked', $group['allow_visit']);

			$this->assign('allowAskRadios', $boolRadios);
			$this->assign('allowAskChecked', $group['allow_ask']);

			$this->assign('allowEditQuestionRadios', $boolRadios);
			$this->assign('allowEditQuestionChecked', $group['allow_edit_question']);

			$this->assign('allowDeleteQuestionRadios', $boolRadios);
			$this->assign('allowDeleteQuestionChecked', $group['allow_delete_question']);

			$this->assign('allowStarQuestionRadios',  $boolRadios);
			$this->assign('allowStarQuestionChecked', $group['allow_star_question']);

			$this->assign('allowEmailQuestionRadios',  $boolRadios);
			$this->assign('allowEmailQuestionChecked', $group['allow_email_question']);

			$this->assign('allowAnswerRadios', $boolRadios);
			$this->assign('allowAnswerChecked', $group['allow_answer']);
			
			$this->assign('allowAnswerOwnQuestionRadios', $boolRadios);
			$this->assign('allowAnswerOwnQuestionChecked', $group['allow_answer_own_question']);
			
			$this->assign('allowAnswerResolvedQuestionRadios', $boolRadios);
			$this->assign('allowAnswerResolvedQuestionChecked', $group['allow_answer_resolved_question']);
			
			$this->assign('allowMultipleAnswerRadios', $boolRadios);
			$this->assign('allowMultipleAnswerChecked', $group['allow_multiple_answer']);
			
			$this->assign('allowEditAnswerRadios', $boolRadios);
			$this->assign('allowEditAnswerChecked', $group['allow_edit_answer']);

			$this->assign('allowDeleteAnswerRadios', $boolRadios);
			$this->assign('allowDeleteAnswerChecked', $group['allow_delete_answer']);

			$this->assign('allowCommentRadios', $boolRadios);
			$this->assign('allowCommentChecked', $group['allow_comment']);

			$this->assign('allowEditCommentRadios', $boolRadios);
			$this->assign('allowEditCommentChecked', $group['allow_edit_comment']);

			$this->assign('allowDeleteCommentRadios',  $boolRadios);
			$this->assign('allowDeleteCommentChecked', $group['allow_delete_comment']);

			$this->assign('allowVoteRadios',  $boolRadios);
			$this->assign('allowVoteChecked', $group['allow_vote']);

			$this->assign('allowRateRadios',  $boolRadios);
			$this->assign('allowRateChecked', $group['allow_rate']);

			$this->assign('allowFollowQuestionRadios',  $boolRadios);
			$this->assign('allowFollowQuestionChecked', $group['allow_follow_question']);

			$this->assign('allowFollowUserRadios',  $boolRadios);
			$this->assign('allowFollowUserChecked', $group['allow_follow_user']);

			$this->assign('allowReportRadios',  $boolRadios);
			$this->assign('allowReportChecked', $group['allow_report']);

			$this->assign('allowSearchRadios',  $boolRadios);
			$this->assign('allowSearchChecked', $group['allow_search']);

			$this->assign('allowViewProfileRadios',  $boolRadios);
			$this->assign('allowViewProfileChecked', $group['allow_view_profile']);

			$this->assign('allowInvisibleRadios',  $boolRadios);
			$this->assign('allowInvisibleChecked', $group['allow_invisible']);

			$this->assign('allowCreateTeamRadios',  $boolRadios);
			$this->assign('allowCreateTeamChecked', $group['allow_create_team']);

			$this->assign('rs', $group);
			$this->display();
		}
	}


	//删除用户组
	function deleteUserGroup()
	{
		
		$groupID   = isset($_GET['group_id']) ? intval($_GET['group_id']) : 0;
		$group = $this->UserGroup->find($groupID);

		$this->assign('jumpUrl', __URL__.'/manageUserGroup');

		if ('system' == $group['type'])
		{
			$this->error(L('group.group_deleted_illegal'));
		}
		if (isset($_POST['action']['confirm']))
		{
			$this->UserGroup->where('group_id='.$groupID)->delete();
			unlink(DATA_PATH.'cache_usergroup_'.$groupID.'.php');

			$this->success(L('group.group_deleted_succeed'));
		}
		elseif (isset($_POST['action']['cancel']))
		{
		    $this->redirect('Admin-Group/manageUserGroup');
		}
		else
		{
		    $this->assign('action', __ACTION__);
            $this->confirm(L('group.group_deleted_confirm'));
		}
	}


	//编辑管理用户组
	public function editAdminGroup()
	{
		
		$groupID = intval($_REQUEST['group_id']);
		$userGroup  = $this->UserGroup->find($groupID);
		$adminGroup = $this->AdminGroup->find($groupID);

		if (empty($adminGroup))
		{
			$this->error(L('group.group_does_not_exist'));
		}

		if ($this->isPost())
		{
			$this->AdminGroup->create();
			$this->AdminGroup->save();
			$this->AdminGroup->cacheAdminGroup($groupID);
			$this->success(L('group.group_updated_succeed'));
		}
		else
		{
			$boolRadios = array(1 => L('yes'), 0 => L('no'));

			$this->assign('allowEditQuestionRadios', $boolRadios);
			$this->assign('allowEditQuestionChecked', $adminGroup['allow_edit_question']);

			$this->assign('allowDeleteQuestionRadios', $boolRadios);
			$this->assign('allowDeleteQuestionChecked', $adminGroup['allow_delete_question']);

			$this->assign('allowEditAnswerRadios', $boolRadios);
			$this->assign('allowEditAnswerChecked', $adminGroup['allow_edit_answer']);

			$this->assign('allowDeleteAnswerRadios', $boolRadios);
			$this->assign('allowDeleteAnswerChecked', $adminGroup['allow_delete_answer']);

			$this->assign('allowEditCommentRadios', $boolRadios);
			$this->assign('allowEditCommentChecked', $adminGroup['allow_edit_comment']);

			$this->assign('allowDeleteCommentRadios', $boolRadios);
			$this->assign('allowDeleteCommentChecked', $adminGroup['allow_delete_comment']);

			$this->assign('allowApprovePostRadios',  $boolRadios);
			$this->assign('allowApprovePostChecked', $adminGroup['allow_approve_post']);
			
			$this->assign('allowApproveTeamRadios',  $boolRadios);
			$this->assign('allowApproveTeamChecked', $adminGroup['allow_approve_team']);

			$this->assign('allowApproveUserRadios',  $boolRadios);
			$this->assign('allowApproveUserChecked', $adminGroup['allow_approve_user']);
			
			$this->assign('allowEditUserRadios',  $boolRadios);
			$this->assign('allowEditUserChecked', $adminGroup['allow_edit_user']);

			$this->assign('allowBanUserRadios',  $boolRadios);
			$this->assign('allowBanUserChecked', $adminGroup['allow_ban_user']);

			$this->assign('allowViewIpRadios',  $boolRadios);
			$this->assign('allowViewIpChecked', $adminGroup['allow_view_ip']);

			$this->assign('allowBanIpRadios',  $boolRadios);
			$this->assign('allowBanIpChecked', $adminGroup['allow_ban_ip']);

			$this->assign('allowAnnounceRadios',  $boolRadios);
			$this->assign('allowAnnounceChecked', $adminGroup['allow_announce']);

			$this->assign('allowViewLogRadios',  $boolRadios);
			$this->assign('allowViewLogChecked', $adminGroup['allow_view_log']);

			$this->assign('userGroup', $userGroup);
			$this->assign('rs', $adminGroup);

			$this->display();
		}
	}

}
//End Class

/* End of file GroupAction.class.php */
/* Location: ./App/Lib/Action/Admin/GroupAction.class.php */
